package com.data.reports.export.excel;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;

public class ViewExcelUtil {

    public static void buildExcelDocument(String fileName, Workbook workbook, HttpServletResponse response)
            throws Exception {
        fileName = URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名/**/
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Transfer-Encoding", "binary");
        if (StringUtils.isBlank(fileName)) {
            response.setHeader("Content-disposition", "attachment;");
        } else {
            response.setHeader("Content-disposition", "attachment;fileName=" + fileName);
        }
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
    }

}